var pageSize=20;  //每页显示的记录条数
var curPage=0;   //显示第curPage页
var len;         //总行数
var page;        //总页数

$(function () {

    $("body").css("opacity","1")

    $("#xjxxcx").click(function(){
        $(this).css({"background-color":"#007dd2","color":"white"})
        $("#xjxxxg").css({"background-color":"white","color":"black"})
        $("#xjxxxz").css({"background-color":"white","color":"black"})
        $("#kongzhi1").show(500)
        $("#kongzhi2").hide(500)
        $("#kongzhi3").hide(500)
    });


    $("#xjxxxg").click(function(){
        $(this).css({"background-color":"#007dd2","color":"white"})
        $("#xjxxcx").css({"background-color":"white","color":"black"})
        $("#xjxxxz").css({"background-color":"white","color":"black"})
        $("#kongzhi2").show(500)
        $("#kongzhi1").hide(500)
        $("#kongzhi3").hide(500)
    });


    $("#xjxxxz").click(function(){
        $(this).css({"background-color":"#007dd2","color":"white"})
        $("#xjxxxg").css({"background-color":"white","color":"black"})
        $("#xjxxcx").css({"background-color":"white","color":"black"})
        $("#kongzhi3").show(500)
        $("#kongzhi2").hide(500)
        $("#kongzhi1").hide(500)
    });


    $.post("/xjxt/biyeshejichax",function (res) {

        var hehe = JSON.parse(res);
        console.log(hehe);
        $("#xuejixinxi").append("<tr id='first'>" + "<td>" + "学生学号" + "</td>" + "<td>" + "姓名" + "</td>" + "<td>" + "老师工号" + "</td>" + "<td>" + "老师姓名" + "</td>" + "<td>" + "删除" + "</td>" + "</tr>");
        $.each(hehe, function (colIndex, col) {
            var th;
            th += "<td>" + "<a id='" + col.username + "' href='#'>" + col.username + "</a>" + "</td>" + "<td>" + col.name + "</td>" + "<td>" + col.laoshi + "</td>" + "<td>" + col.lsname + "</td>" + "<td>" + "<button id='1" + col.username + "'>" + "删除" + "</button>" + "</td>";
            $("#xuejixinxi").append("<tr>" + th + "</tr>");

            $("#"+col.username+"").click(function () {

                $("#xsxh").val(col.username);
                $("#xsxm").val(col.name);
                $("#lsgh").val(col.laoshi);
                $("#lsxm").val(col.lsname);

                $("#xjxxxg").css({"background-color":"#007dd2","color":"white"})
                $("#xjxxcx").css({"background-color":"white","color":"black"})
                $("#xjxxxz").css({"background-color":"white","color":"black"})
                $("#kongzhi2").show(500)
                $("#kongzhi1").hide(500)
                $("#kongzhi3").hide(500)

                $("#xjxganniu").click(function () {

                    var r = confirm("是否修改该毕业设计信息");
                    console.log(r);
                    if(r == true) {
                        $.post("/xjxt/glybiyeshejixiugai", {
                            "username": $("#xsxh").val(),
                            "name": $("#xsxm").val(),
                            "laoshi": $("#lsgh").val(),
                            "lsname": $("#lsxm").val(),
                            "pusername": col.username
                        }, function (res) {
                            if (res == "2") {
                                alert("修改失败");
                                window.location.reload()
                            }
                            if (res == "1") {
                                alert("修改成功");
                                window.location.reload()
                            }

                        });
                    }

                });

            });



            $("#1"+col.username+"").click(function () {

                var r = confirm("是否删除该学籍信息");
                console.log(r);
                if(r == true)
                {
                    $.post("/xjxt/biyeshejishanchu?username="+ col.username +"",function(res){
                        if(res == 1){
                            alert("恭喜您删除成功")
                            window.location.reload()
                        }
                        else{
                            alert("删除失败")
                            window.location.reload()
                        }
                    });
                }

            });


        });

        len =$("#xuejixinxi tr").length-1;   //去掉表头
        page=len % pageSize==0 ? len/pageSize : Math.floor(len/pageSize)+1;//根据记录条数，计算页数
        console.log("len:"+len+"page:"+page);
        document.getElementById("page").value=page;
        curPage=1;
        displayPage();//显示第一页
        $("#nextpage").click(function(){//下一页
            if(curPage<page){
                curPage+=1;
            }
            else{
                alert("已经是最后一页了");
            }
            displayPage();
        });
        $("#lastpage").click(function(){//上一页
            if(curPage>1){
                curPage-=1;
            }
            else{
                alert("已经是第一页了");
            }
            displayPage();
        });
        $("#npage").click(function(){//跳到固定某一页
            var npage=parseInt(document.getElementById("curPage").value);
            if(npage>page||npage<1){
                alert("该页不存在");
            }
            else{
                curPage=npage;
            }
            displayPage();
        });
    });

    $("#xuejixinz").click(function () {

        var r = confirm("是否新增该毕业设计信息");
        console.log(r);
        if(r == true) {
            $.post("/xjxt/biyeshejixinzeng", {
                "username": $("#xsxh1").val(),
                "name": $("#xsxm1").val(),
                "laoshi": $("#lsgh1").val(),
                "lsname": $("#lsxm1").val()
            }, function (res) {
                if (res == "2") {
                    alert("新增失败");
                    window.location.reload()
                }
                if (res == "1") {
                    alert("新增成功");
                    window.location.reload()
                }

            });
        }

    });



    $("#xueshengss1").change(function () {
        var searchText = $(this).val();//获取输入的搜索内容
        if (searchText != "") {

            $("#xuejixinxi tr:not('#first')").hide().filter(":contains('"+searchText+"')").show();
        }
        else {
            $("#xuejixinxi tr").show();
        }
    })
    $("#xueshengss").click(function () {
        $("searchText").change();
    })

});
function displayPage(){
    var begin=(curPage-1)*pageSize;//起始记录号
    var end = begin + pageSize;
    console.log("  begin:"+len+"   end:"+end);
    if(end > len ) end=len;
    $("#xuejixinxi tr").hide();
    $("#xuejixinxi tr").each(function(i){
        if(i-1>=begin && i-1<end)//显示第page页的记录
        {
            $("#first").show();
            $(this).show();
            document.getElementById("curPage").value=curPage;
        }
    });

}

function pageSize2(){
    curPage=0;   //显示第curPage页
    pageSize=parseInt(document.getElementById("pageSize").value);
    len =$("#xuejixinxi tr").length-1;   //去掉表头
    page=len % pageSize==0 ? len/pageSize : Math.floor(len/pageSize)+1;//根据记录条数，计算页数
    console.log("len:"+len+"   page:"+page);
    document.getElementById("page").value=page;
    curPage=1;
    displayPage();//显示第一页
}
